class CloneView extends egret.Sprite{
	public static CLONEVIEW:string="cloneview";//设置的切换场景
	private backdropGame = new BackdropGame();//背景
	private homeGame = new HomeGame();//房子
	private peopleGame = new PeopleGame();//人物
	private sideGame = new SideGame();//外卖盒子
	private textGame = new TextGame();//分数
	// private butGame = new ButGame();//按钮层
	private darknessView = new BackgroundStart();//遮盖层
	private butView = new ButStart();//按钮层
	private fooDom:egret.Bitmap;//正确餐盒
	private errFooDom:egret.Bitmap;//错误餐盒
	private isY2:boolean = false;//是否在点击区间 鼠标抬起的时候用来判断是否正确 正确则出现动画错误则游戏结束
	private isTouch:boolean = false;//定时器监控是否点击 当没有点击的时候执行定时器的判断是否超过位子
	private PoinTime;// 时间，用来判断点击事是否超过
	private animtTime=75;//定义动画执行的时间
	private Allbut:egret.Shape;//按钮
	private isClick=true;//用来取消点击
	private dieCont="";//死亡提示
	private isLongClick=false;//判断是否长时间点击
	public addSpeed:egret.Timer = new egret.Timer(10000,0);//定时器创建房子
	private coloNum=0;
	private sppp=4;
	private antNume=false;
	private botom = RES.getRes("botom_png");//底部
	private redBut = RES.getRes("redbut_png");//红色按钮
	private redEnd = RES.getRes("redend_png");//红色按钮按下
	private yelBut = RES.getRes("yelbut_png");//黄色按钮
	private yelEnd = RES.getRes("yelend_png");//黄色按钮按下
	private grenBut = RES.getRes("grenbut_png");//绿色按钮
	private grenEnd = RES.getRes("grenend_png");//绿色按钮按下
	public ButRed:egret.Bitmap;//红色
	public ButGreen:egret.Bitmap;//绿色
	public ButYel:egret.Bitmap;//黄色
	public constructor() {
		super();
	};
	// 初始化游戏
	public init(){
		// 添加背景 并启动背景运动的定时器
		this.addChild(this.backdropGame);
		this.backdropGame.start();
		
		// 添加房子 并启动创建房子和移动的定时器
		this.addChild(this.homeGame);
		this.homeGame.starTime();
		this.homeGame.HoustTime();

		// 添加人物
		this.addChild(this.peopleGame);
		// 添加外卖盒子
		this.addChild(this.sideGame);
		// 正确的餐盒和错误的餐盒
		this.fooDom = this.sideGame.correctWai;
		this.errFooDom = this.sideGame.errWai1;

		// 分数
		this.addChild(this.textGame);
		this.addEventListener(egret.Event.ADDED_TO_STAGE,this.inits,this);
 
		// 启动定时器
		this.startEnt();
		this.HoustTime()
	};
	private inits() {
		// 黄色按钮
		this.ButYel= new egret.Bitmap(); 
		this.ButYel.texture = this.yelBut;
		this.ButYel.width = this.ButYel.texture.textureWidth / 2;
		this.ButYel.height = this.ButYel.texture.textureHeight / 2;
		this.ButYel.x = (this.stage.stageWidth - this.ButYel.width) / 2 ;
		this.ButYel.y= this.stage.stageHeight- this.ButYel.height * 3;
		this.addChild(this.ButYel);

		// 红色按钮
		this.ButRed= new egret.Bitmap(); 
		this.ButRed.texture = this.redBut;
		this.ButRed.width = this.ButRed.texture.textureWidth / 2;
		this.ButRed.height = this.ButRed.texture.textureHeight / 2;
		this.ButRed.x = this.ButYel.x - 80;
		this.ButRed.y= this.ButYel.y + this.ButRed.height / 1.5;
		this.addChild(this.ButRed);

		// 绿色按钮
		this.ButGreen= new egret.Bitmap(); 
		this.ButGreen.texture = this.grenBut;
		this.ButGreen.width = this.ButGreen.texture.textureWidth / 2;
		this.ButGreen.height = this.ButGreen.texture.textureHeight / 2;
		this.ButGreen.x =  this.ButYel.x + 80 ;
		this.ButGreen.y= this.ButRed.y;
		this.addChild(this.ButGreen);

		// 开启点击事件
		this.ButRed.touchEnabled = true;
		this.ButGreen.touchEnabled = true;
		this.ButYel.touchEnabled = true;
		this.ButRed.addEventListener(egret.TouchEvent.TOUCH_BEGIN,()=>{
			this.ButRed.texture = this.redEnd;
		},this);//鼠标按下
		this.ButRed.addEventListener(egret.TouchEvent.TOUCH_END,()=>{
			this.ButRed.texture = this.redBut;
		},this);//鼠标松开


		this.ButGreen.addEventListener(egret.TouchEvent.TOUCH_BEGIN,()=>{
			this.ButGreen.texture = this.grenEnd;
		},this);//鼠标按下
		this.ButGreen.addEventListener(egret.TouchEvent.TOUCH_END,()=>{
			this.ButGreen.texture = this.grenBut;
		},this);//鼠标松开
		
		
		this.ButYel.addEventListener(egret.TouchEvent.TOUCH_BEGIN,()=>{
			this.ButYel.texture = this.yelEnd;
		},this);//鼠标按下
		this.ButYel.addEventListener(egret.TouchEvent.TOUCH_END,()=>{
			this.ButYel.texture = this.yelBut;
		},this);//鼠标松开

		this.ButRed.addEventListener(egret.TouchEvent.TOUCH_TAP,()=>{
			if(this.isClick){
				switch (this.homeGame.HomeArr[0].objC){
						case 0:
						
							this.cuowu();
							break;
						case 1:
							this.sideGame.correctWai.texture=this.sideGame.redBox;
							this.zhengque();
							break;
						case 2:
						
							this.cuowu();
							break;
						default:
							break;	
				};
				this.peopleGame.people.addEventListener(egret.Event.COMPLETE, (e:egret.Event)=>{
						// 恢复骑车动画
						if(this.antNume==false){

							this.peopleGame.people.gotoAndPlay("start",-1);
						}
				}, this);
			}	
		},this);
		this.ButGreen.addEventListener(egret.TouchEvent.TOUCH_TAP,()=>{
			if(this.isClick){
				switch (this.homeGame.HomeArr[0].objC){
						case 0:
						
							this.cuowu();
							break;
						case 1:
						
							this.cuowu();
							break;
						case 2:
							this.sideGame.correctWai.texture=this.sideGame.grenBox;
							this.zhengque();
							break;
						default:
							break;	
				};
				this.peopleGame.people.addEventListener(egret.Event.COMPLETE, (e:egret.Event)=>{
						// 恢复骑车动画
						if(this.antNume==false){

							this.peopleGame.people.gotoAndPlay("start",-1);
						}
				}, this);
			}	
		},this);
		this.ButYel.addEventListener(egret.TouchEvent.TOUCH_TAP,()=>{
			if(this.isClick){	
				switch (this.homeGame.HomeArr[0].objC){
						case 0:
							this.sideGame.correctWai.texture=this.sideGame.yelBox;
							this.zhengque();
							break;
						case 1:
						
							this.cuowu();
							break;
						case 2:
						
							this.cuowu();
							break;
						default:
							break;	
				};
				this.peopleGame.people.addEventListener(egret.Event.COMPLETE, (e:egret.Event)=>{
						// 恢复骑车动画
						if(this.antNume==false){

							this.peopleGame.people.gotoAndPlay("start",-1);
						}
				}, this);
			}	
		},this);
	};
	// 加速度
	public HoustTime() {
		this.addSpeed.addEventListener(egret.TimerEvent.TIMER,this.addSpends,this);
		this.addSpeed.start();
	};
	// 加速度函数
	private addSpends() {
		this.sppp++;
		this.homeGame.speed=this.sppp;
		this.sideGame.spend=this.sppp; 
		this.backdropGame.imgAdd=this.sppp;
		if(this.animtTime > 10){
			this.animtTime -= this.sppp*2;
		}
		if(this.sppp == 15){
			this.addSpeed.stop();
		}
	}
	// 帧动画监视没有操作时候的判断
	private startEnt() {
		this.addEventListener(egret.Event.ENTER_FRAME,this.addEve,this);
	};
	// 监视事件逻辑
	private addEve() {
		if(this.homeGame.HomeArr.length > 0 ){
			// 房子的Y轴
			let f1 = this.homeGame.HomeArr[0].house.y;
			// 
			let f2 = this.homeGame.HomeArr[0].house.y+this.homeGame.HomeArr[0].house.height / 3
			// 绿色显示
			let f3 = this.homeGame.HomeArr[0].house.y+(this.homeGame.HomeArr[0].house.height / 3) * 2;
			// 房子的Y2
			let f4 = this.homeGame.HomeArr[0].house.y+this.homeGame.HomeArr[0].house.height;
			// 人的Y轴
			let P1 = this.peopleGame.people.y;
			// 人的定点位置 中间
			let p2 = this.peopleGame.people.y;
			// 人的底部
			let p3 = this.peopleGame.people.y + this.peopleGame.people.height*0.7;
			// 房子位置达到人的顶部 提示条变成黄色
			if(f4 >= p2 && f1 < p2){
				switch (this.homeGame.HomeArr[0].objC){
					case 0:
						this.homeGame.HintArr[0].texture=this.homeGame.Yellow;
						break;
					case 1:
						this.homeGame.HintArr[0].texture=this.homeGame.Red;
						break;
					case 2:
						this.homeGame.HintArr[0].texture=this.homeGame.Green;
						break;
					default:
						break;	
				};
				this.coloNum = 2
				
		}else if(f1 > p3){
				// console.log("红色")
				this.coloNum = 0
				this.homeGame.HintArr[0].texture=this.homeGame.Err;
			}
			// 游戏死亡  第一个房子的属性为假  没有点击事件  房子的顶部大于人的中间部位
			if(this.homeGame.HomeArr[0].isTrue == false && this.isTouch == false && f1 >= p3 ){
				this.end();
			};
			if(this.isLongClick){
				if(f1 >= p2 ){
					this.isLongClick=false;
					this.end();
				}
			}
		}
	};
	private cuowu() {
		switch (this.homeGame.HomeArr[0].objX){
					case 0:
						this.peopleGame.people.gotoAndPlay("left",1);
						setTimeout( ()=>{this.errLeft()},300);
						break;
					case 1:
						this.peopleGame.people.gotoAndPlay("right",1);
						setTimeout( ()=>{this.errRight()},300);
						break;
					default:
						break;	
		};
	}
	private zhengque() {
		this.homeGame.HomeArr[0].isTrue = true;
				switch (this.homeGame.HomeArr[0].objX){
					case 0:
						this.peopleGame.people.gotoAndPlay("left",1);
						setTimeout( ()=>{
							this.animtLeft();
							this.textGame.textTitle.texture = this.textGame.preImg;
							this.textGame.textTitle.visible = true;
							this.ScoreText();
						},300);
						break;
					case 1:
						this.peopleGame.people.gotoAndPlay("right",1);
						setTimeout( ()=>{
							this.animtRight();
							this.textGame.textTitle.texture = this.textGame.preImg;
							this.textGame.textTitle.visible = true;
							this.ScoreText();
						},300);
						break;
					default:
						break;	
		};
	}
	/** 成功 */

	// 正确执行动画
	private animtLeft() {
		this.fooDom.x=151;
		this.fooDom.y=400;
		this.fooDom.visible=true;
		let x;
		let y;
		// 给物体找到位置
		if(this.homeGame.HomeArr[0].num == 0){
			x=this.homeGame.HomeArr[0].house.x + this.homeGame.HomeArr[0].house.width * 0.65;
			y=this.homeGame.HomeArr[0].house.y + this.homeGame.HomeArr[0].house.height * 0.06;
		}else if(this.homeGame.HomeArr[0].num == 1){
			x=this.homeGame.HomeArr[0].house.x + this.homeGame.HomeArr[0].house.width * 0.65;
			y=this.homeGame.HomeArr[0].house.y + this.homeGame.HomeArr[0].house.height * 0.15;
		}else{
			x=this.homeGame.HomeArr[0].house.x + this.homeGame.HomeArr[0].house.width * 0.32;
			y=this.homeGame.HomeArr[0].house.y + this.homeGame.HomeArr[0].house.height * 0.07;
		}
		// 执行抛物线传入参数 物体：外卖盒子 X轴 Y轴
		this.parabolaLeft(this.fooDom,x,y+this.homeGame.speed*(this.animtTime*2 / 10));
		setTimeout(()=>{
			// 玻璃动画执行
			this.culleAntmin(x,y+this.homeGame.speed*(this.animtTime*2 / 10));
		},this.animtTime*4)
	};
	private animtRight() {
		this.fooDom.x=151;
		this.fooDom.y=400;
		this.fooDom.visible=true;
		let x;
		let y;
		// 给物体找到位置
		if(this.homeGame.HomeArr[0].num == 0){
			x=this.homeGame.HomeArr[0].house.x - this.homeGame.HomeArr[0].house.width + this.homeGame.HomeArr[0].house.width * 0.10;
			y=this.homeGame.HomeArr[0].house.y + this.homeGame.HomeArr[0].house.height * 0.06;
		}else if(this.homeGame.HomeArr[0].num == 1){
			x=this.homeGame.HomeArr[0].house.x - this.homeGame.HomeArr[0].house.width + this.homeGame.HomeArr[0].house.width * 0.1;
			y=this.homeGame.HomeArr[0].house.y + this.homeGame.HomeArr[0].house.height * 0.15;
		}else{
			x=this.homeGame.HomeArr[0].house.x - this.homeGame.HomeArr[0].house.width + this.homeGame.HomeArr[0].house.width * 0.46;
			y=this.homeGame.HomeArr[0].house.y + this.homeGame.HomeArr[0].house.height * 0.07;
		}
		this.parabolaRight(this.fooDom,x,y+this.homeGame.speed*(this.animtTime*2 / 10));
		setTimeout(()=>{
			this.culleAntmin(x,y+this.homeGame.speed*(this.animtTime*2 / 10));
		},this.animtTime*4);
	};
	// 正确执行完之后，到达位置玻璃动画出现然后切换房子
	private culleAntmin(x,y) {
		// 外卖消失
		this.fooDom.visible=false;
		// 碎玻璃动画显示
		this.sideGame.cullet.visible=true;
		this.sideGame.cullet.x=x;
		this.sideGame.cullet.y=y;
		// 动画播放
		this.sideGame.cullet.gotoAndPlay("start",1);
		// 房子更换图片
		if(this.homeGame.HomeArr.length==0){
			console.log(1)
		}else{

			this.homeGame.HomeArr[0].house.texture= this.homeGame.HomeArr[0].change;
		}
		this.isTouch = false;
		
		this.sideGame.cullet.addEventListener(egret.Event.COMPLETE, (e:egret.Event)=>{

					this.sideGame.cullet.visible=false;
		}, this);
	};
	
	/** 失败 */


	// 错误执行动画  错误的外卖盒子显示出来  X轴和Y轴定位，然后开始执行移动动画
	private errLeft() {
		this.errFooDom.visible = true;
		this.parabolaLeft(this.errFooDom,40,this.peopleGame.people.y);
		setTimeout(()=>{
			this.errFooNone(40,450);
		},this.animtTime*4)
	};
	private errRight() {
		this.errFooDom.visible = true;
		this.parabolaRight(this.errFooDom,300,this.peopleGame.people.y);
		setTimeout(()=>{
			this.errFooNone(300,450);
		},this.animtTime*4)
	};

	// 错误的时候原物小时动画显示之一次之后消失
	private errFooNone(x,y) {
		// console.log("需要消失");
		// 盘子消失
		this.errFooDom.visible = false;
		// 盘子动画显示
		this.sideGame.errWai.visible=true;
		this.sideGame.errWai.x=x;
		this.sideGame.errWai.y=y;
		// 动画播放
		this.sideGame.errWai.gotoAndPlay("start",1);
		// 动画移动
		this.sideGame.sideTime();
		// 提示消失掉
		setTimeout(()=>{this.noneTitle();},800); 
		this.sideGame.errWai.addEventListener(egret.Event.COMPLETE, (e:egret.Event)=>{
				this.end();
				this.isTouch = false;
		}, this);
	};


	/** 公共 */

	// 左边抛物线
	private parabolaLeft(e,a,b){
		var tws = egret.Tween.get(e);
		tws.to({x:130,y:300},this.animtTime,egret.Ease.backInOut)
		   .to({x:100,y:200},this.animtTime,egret.Ease.backInOut)
		   .to({x:70,y:250},this.animtTime,egret.Ease.backInOut)
		   .to({x:a,y:b},this.animtTime,egret.Ease.backInOut);
	};
	private parabolaRight(e,a,b) {
		var tws = egret.Tween.get(e);
		tws.to({x:190,y:300},this.animtTime,egret.Ease.backInOut)
		.to({x:240,y:200},this.animtTime,egret.Ease.backInOut)
		.to({x:270,y:250},this.animtTime,egret.Ease.backInOut)
		.to({x:a,y:b},this.animtTime,egret.Ease.backInOut);
	};
	
	// 分数
	private ScoreText() {

		if(this.homeGame.HomeArr[0].num == 0){
			this.textGame.addNum(3);
		}else if(this.homeGame.HomeArr[0].num == 1){
			this.textGame.addNum(6);
		}else if(this.homeGame.HomeArr[0].num == 2){
			this.textGame.addNum(10);
		};
		// 提示消失掉
		setTimeout(()=>{this.noneTitle();},800); 
	};
	
	
	// 提示消失
	private noneTitle() {
		this.textGame.textTitle.visible = false;
	};

	// 重新开始
	private startPlay(){
		this.antNume=false;
		// event.stopImmediatePropagation();
		var changeEvent = new ChangeEvent(ChangeEvent.CHANGE_EVENT);
		changeEvent.eventType = GameagentView.GAMEAGENT_VIEW;
		changeEvent.obj = this;
		SwitcherView.getInstance().dispatchEvent(changeEvent);
	};

	// 游戏结束
	private end(){
	
		this.isClick=false;
		this.textGame.textTitle.texture = this.textGame.endImg;
		this.textGame.textTitle.visible = true;
		this.backdropGame.end();//背景结束
		this.homeGame.end();//房子结束
		this.peopleGame.people.gotoAndPlay("stop",1);
		this.antNume=true;
		this.removeEventListener(egret.Event.ENTER_FRAME,this.addEve,this);//结束监控
		this.addChild(this.darknessView);//遮盖层
		this.addChild(this.butView);//按钮层
	
		this.butView.anew();
		this.butView.anewBut.touchEnabled = true;
		this.butView.anewBut.addEventListener(egret.TouchEvent.TOUCH_TAP,this.startPlay,this);//开始
	};
	// 结束此场景
	public endView(){
		if(this.butView.anewBut.hasEventListener(egret.TouchEvent.TOUCH_TAP))
			this.butView.anewBut.removeEventListener(egret.TouchEvent.TOUCH_TAP,this.startPlay,this);
	};
}